﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;
using SqliteWrapper.Query;
using BartonExtension;

namespace SqliteWrapper
{
    public class QueryWrapper
    {
        public string DBPath { get; set; }

        public static QueryWrapper Create(string db_path)
        {
            return new QueryWrapper() { DBPath = db_path };
        }

        public object GetAlbums(List<SQLiteParameter> conditionCollection,int? startIndex, int? length,DateTime? startTime,DateTime? endTime,QueryOrder order)
        {
            var query = new Q_Album();
            DataTable result;
            SqliteError error = query.Query(out result, DBPath, conditionCollection, startIndex, length, startTime, endTime,order);
            if (error.IsProcessSuccess && result != null)
                error.Response = result;
            else
                error.IsProcessSuccess = false;
            return error;
        }

        public object GetCategories(List<SQLiteParameter> conditionCollection, int? startIndex, int? length, DateTime? startTime, DateTime? endTime,QueryOrder order)
        {
            var query = new Q_Category();
            DataTable result;
            
            SqliteError error = query.Query(out result, DBPath, conditionCollection, startIndex, length, startTime, endTime,order);
            if (error.IsProcessSuccess && result != null)
                error.Response = result;
            else
                error.IsProcessSuccess = false;
            return error;
        }

        public object GetBooks(List<SQLiteParameter> conditionCollection, int? startIndex, int? length, DateTime? startTime, DateTime? endTime,QueryOrder order)
        {
            var query = new Q_Book();
            DataTable result;
            SqliteError error = query.Query(out result, DBPath, conditionCollection, startIndex, length, startTime, endTime,order);
            if (error.IsProcessSuccess && result != null)
            {
                List<Book> books = new List<Book>();
                foreach (DataRow row in result.Rows)
                {
                    Book book = new Book()
                    {
                        ID = row["ID"].toInt64().Value,
                        AlbumId = row["AlbumId"].toInt64().Value,
                        CategoryId = row["CategoryId"].toInt64().Value,
                        Score = row["Score"].toDouble().Value,
                        Version = row["Version"].toFloat().Value,
                        Title = row["Title"].ToString(),
                        Author = row["Author"].ToString(),
                        SourcePath = "{0}/uploads/{1}".FormatWith(ConfigurationExtensions.ValueOfConfig("Host"), row["networkPath"].ToString()),
                        Thumbnail = "{0}/uploads/thumbs/{1}".FormatWith(ConfigurationExtensions.ValueOfConfig("Host"), row["thumbnailUrl"].ToString()),
                        Desc = row["Desc"].ToString(),
                        CreateTimeTag = row["CreateTimeTag"].ToString()
                    };
                    books.Add(book);
                }
                error.Response = books;
            }
            else
                error.IsProcessSuccess = false;
            return error;
        }

        public object GetComments(List<SQLiteParameter> conditionConllection, int? startIndex, int? length, DateTime? startTime, DateTime? endTime, QueryOrder order)
        {
            var query = new Q_Comment();
            DataTable result;
            SqliteError error = query.Query(out result, DBPath, conditionConllection, startIndex, length, startTime, endTime, order);
            if (error.IsProcessSuccess && result != null)
                error.Response = result;
            else
                error.IsProcessSuccess = false;
            return error;
        }

        public object GetData(string query)
        {
            QueryCommon q = new QueryCommon();
            DataTable table;
            SqliteError error = q.Query(query, out table,DBPath);
            if (error.IsProcessSuccess && table != null)
                error.Response = table;
            else
                error.IsProcessSuccess = false;
            return error;
        }
    }
}
